from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from .models import DicType, DicItem, Platform, Menu
from django.contrib.auth import get_user_model

# 获取用户模型
User = get_user_model()

class CommonInfoAdmin(admin.ModelAdmin):
    readonly_fields = ('get_create_user_info', 'get_update_user_info', 'update_time', 'deleted_time')
    # 保存模型时设置创建人和更新人
    def save_model(self, request, obj, form, change):
        if not change:
            obj.create_user = request.user.id
        obj.update_user = request.user.id
        super().save_model(request, obj, form, change)
    
    # 获取用户信息（通用方法）
    def _get_user_info(self, user_id, field_name=''):
        if not user_id:
            return '未知用户（若新增数据，保存后即存为当前用户）'
        try:
            user = User.objects.get(id=user_id)
            return f'{user.id}（{user.nickname or user.username}）'
        except User.DoesNotExist:
            return '用户不存在或已删除'
    
    # 获取创建人信息
    def get_create_user_info(self, obj):
        return self._get_user_info(obj.create_user, '创建人')
    get_create_user_info.short_description = '创建人'
    get_create_user_info.admin_order_field = 'create_user'
    
    # 获取更新人信息
    def get_update_user_info(self, obj):
        return self._get_user_info(obj.update_user, '更新人')
    get_update_user_info.short_description = '更新人'
    get_update_user_info.admin_order_field = 'update_user'

# Register your models here.
@admin.register(DicType)
class DicTypeAdmin(CommonInfoAdmin):
    list_display = ('dic_type_id', 'dic_type_code', 'dic_type_name', 'description', 'create_time', 'update_time')
    search_fields = ('dic_type_id', 'dic_type_code', 'dic_type_name', 'description')
    list_filter = ('create_time', 'update_time')
    fields = ('dic_type_id', 'dic_type_code', 'dic_type_name', 'description', 'remark', 'sort_num', 'data_remark', 'get_create_user_info', 'get_update_user_info', 'be_deleted', 'deleted_time', 'valid_status')

@admin.register(DicItem)
class DicItemAdmin(MPTTModelAdmin, CommonInfoAdmin):
    list_display = ('dic_item_id', 'dic_item_code', 'dic_item_name', 'dic_type_id', 'description', 'remark', 'create_time', 'update_time')
    search_fields = ('dic_item_id', 'dic_item_code', 'dic_item_name', 'dic_type_id', 'description')
    list_filter = ('create_time', 'update_time')
    mptt_level_indent = 20 # 设置层级缩进
    mptt_indent_field = 'dic_item_name' # 设置层级字段
    fields = ('dic_type_id', 'dic_item_id', 'dic_item_code', 'dic_item_name', 'description', 'remark', 'parent_id', 'parent_dic_item_id', 'level_num', 'sort_num', 'data_remark', 'get_create_user_info', 'get_update_user_info', 'be_deleted', 'deleted_time', 'valid_status')
    readonly_fields = ('parent_dic_item_id', 'level_num') + CommonInfoAdmin.readonly_fields # 设置只读字段
    ordering = ('dic_type_id', 'dic_item_id', 'sort_num') # 设置排序字段

@admin.register(Platform)
class PlatformAdmin(CommonInfoAdmin):
    # 设置列表显示字段
    list_display = ('platform_id', 'platform_code', 'platform_name', 'description', 'version', 'remark', 'sort_num', 'create_time', 'update_time')
    # 设置搜索字段
    search_fields = ('platform_id', 'platform_code', 'platform_name', 'description')
    # 设置编辑页面显示字段
    fields = ('platform_id', 'platform_code', 'platform_name', 'description', 'version', 'homepage', 'app_download_url', 'remark','sort_num', 'data_remark', 'get_create_user_info', 'get_update_user_info', 'be_deleted', 'deleted_time', 'valid_status')
    # 排序字段
    ordering = ('sort_num', 'create_time', 'update_time')

@admin.register(Menu)
class MenuAdmin(MPTTModelAdmin, CommonInfoAdmin):
    # 设置列表显示字段
    list_display = ('name', 'url', 'icon', 'menu_type', 'parent_id', 'sort_num', 'platform_id')
    fields = ('name', 'url', 'icon', 'menu_type', 'parent_id', 'sort_num', 'platform_id', 'data_remark', 'get_create_user_info', 'get_update_user_info', 'be_deleted', 'deleted_time', 'valid_status')
    list_filter = ('platform_id', 'menu_type')
    mptt_level_indent = 20 # 设置层级缩进
    mptt_indent_field = 'name' # 设置层级字段